﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Emoney.Cms.Spider;
using System.Text;
using Emoney.Spider.Basic;
using System.Data.SqlClient;


public partial class NewsList : System.Web.UI.Page
{
    public StringBuilder sbNewsList = new StringBuilder();
    public int _CID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int _ID = OTO.ToInt(Request["ID"], 0);
            string _strID = OTO.ToStr(Request["strID"], "");
            if (_ID > 0)
            {
                _strID = _ID.ToString();
            }
            string _ac = OTO.ToStr(Request["ac"], "");
            //del
            #region
            if (_ac == "del" && !string.IsNullOrEmpty(_strID))
            {
                try
                {
                    string[] arrID = _strID.Split(',');
                    for (int i = 0; i < arrID.Length; i++)
                    {
                        System.Data.SqlClient.SqlParameter[] para = new System.Data.SqlClient.SqlParameter[1];
                        para[0] = new System.Data.SqlClient.SqlParameter("@ID", OTO.ToInt(arrID[i], 0));
                        SQLHelper.ExecuteNonQuery(SQLHelper.StrConn, System.Data.CommandType.StoredProcedure, "sp_DelNewsByID", para);
                    }
                }
                catch
                { }
                finally
                {
                    string key = string.Format(CacheKey.News_CACHE_KEY, "DB", "All");
                    Emoney.Spider.Basic.Cache.MoveCache(key);
                }
                Response.Write("<script>alert('删除成功');this.location.href='NewsList.aspx';</script>");
                Response.End();
            }
            #endregion

            BindHtml();
        }
    }

    private void BindHtml()
    {
        int flag = OTO.ToInt(Request["f"], -1);
        string key = string.Format(CacheKey.News_CACHE_KEY, "DB", "All");
        Emoney.Spider.Basic.Cache.MoveCache(key);

        CmsAccess cAcc = new CmsAccess();
        List<CmsEnty> list = new List<CmsEnty>();

        if (flag < 0)
            list = (List<CmsEnty>)cAcc.ListAcc_NewsAll().Where(o => o.FLAG == 0 || o.FLAG == 1).OrderBy(o => o.FLAG).ToList();
        else
            list = (List<CmsEnty>)cAcc.ListAcc_NewsAll().Where(o => o.FLAG == flag).OrderBy(o => o.FLAG).ToList();
        if (list.Count > 0)
        {
            int _totalCount = 0;
            int _pagesize = 10;
            int _totalPage = 0;
            _totalCount = list.Count;
            int page = OTO.ToInt(Request["page"], 1);
            if (_totalCount > 0)
            {
                if ((_totalCount % _pagesize) == 0)
                {
                    _totalPage = _totalCount / _pagesize;
                }
                else
                {
                    _totalPage = (int)(_totalCount / _pagesize) + 1;
                }
            }
            if (page > _totalPage)
            {
                page = _totalPage;
            }
            int _index = (page - 1) * _pagesize;
            int _end = page * _pagesize;
            if (_end > _totalCount)
                _end = _totalCount;

            sbNewsList.AppendLine("<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"data_list_table\">");
            sbNewsList.AppendLine("<tr>");
            sbNewsList.AppendLine("<td height=\"20\" colspan=\"9\"><a href=\"?f=0\">末签发</a>&nbsp;<a href=\"?f=1\">已签发</a></td>");
            sbNewsList.AppendLine("</tr>");
            sbNewsList.AppendLine("<tr>");
            sbNewsList.AppendLine("<th width=\"60\" height=\"25\"><label>全选");
            sbNewsList.AppendLine("<input type=\"checkbox\" name=\"chkIDAll\" id=\"chkIDAll\" value=\"checkbox\" onclick=\"chkAll();\" />");
            sbNewsList.AppendLine("</label></th>");
            sbNewsList.AppendLine("<th width=\"50\">排序</th>");
            sbNewsList.AppendLine("<th>标题（采集源）</th>");
            sbNewsList.AppendLine("<th width=\"150\">出处</th>");
            sbNewsList.AppendLine("<th width=\"100\">发布时间</th>");
            sbNewsList.AppendLine("<th width=\"100\">采集时间</th>");
            sbNewsList.AppendLine("<th width=\"60\">相关股票</th>");
            sbNewsList.AppendLine("<th width=\"50\">状态</th>");
            sbNewsList.AppendLine("<th width=\"50\">操作</th>");
            sbNewsList.AppendLine("</tr>");
            int n = 0;
            for (int i = _index; i < _end; i++)
            {
                sbNewsList.AppendLine("<tr>");
                sbNewsList.AppendLine("<td height=\"20\" align=\"center\"><label>");
                sbNewsList.AppendLine("<input type=\"checkbox\" name=\"chkID\" id=\"chkID_" + n + "\" value=\"" + list[i].ID + "\" />");
                sbNewsList.AppendLine("</label></td>");
                sbNewsList.AppendLine("<td align=\"center\">" + (i + 1) + "</td>");
                sbNewsList.AppendLine("<td><a href=\"NewsContent.aspx?ID=" + list[i].ID + "\">" + list[i].TITLE + "</a>(<a href=\"" + list[i].LINK + "\" target=\"_blank\">源</a>)</td>");
                sbNewsList.AppendLine("<td>" + list[i].SOURCE + "</td>");
                sbNewsList.AppendLine("<td align=\"center\">" + list[i].PTIME + "</td>");
                sbNewsList.AppendLine("<td align=\"center\">" + list[i].CTIME + "</td>");
                sbNewsList.AppendLine("<td align=\"center\">" + list[i].STOCKCODE + "</td>");
                sbNewsList.AppendLine("<td align=\"center\">" + OTO.GetZD(list[i].FLAG) + "</td>");
                if (flag < 1)
                {
                    sbNewsList.AppendLine("<td align=\"center\"><a href=\"NewsEdit.aspx?ID=" + list[i].ID + "\"><img src=\"../images/bt_edit.gif\" alt=\"修改\" title=\"修改\" /></a>");
                    sbNewsList.AppendLine("<img src=\"../images/bt_del.gif\" alt=\"删除\" /></td>");
                }
                else
                {
                    sbNewsList.AppendLine("<td align=\"center\">&nbsp;</td>");
                }
                sbNewsList.AppendLine("</tr>");
                n++;
            }
            sbNewsList.AppendLine("<tr>");
            sbNewsList.AppendLine("<td height=\"20\" colspan=\"9\">" + PageHelper.PageStr(page, _totalCount, _pagesize, Request.Url.AbsoluteUri) + "</td>");
            sbNewsList.AppendLine("</tr>");
            sbNewsList.AppendLine("</table>");
        }

    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string strID = OTO.ToStr(Request["chkID"]);
        Response.Redirect("NewsList.aspx?strID=" + strID + "&ac=del");
        Response.End();
    }
    protected void btnPost_Click(object sender, EventArgs e)
    {
        string strID = OTO.ToStr(Request["chkID"]);
        string[] arrID = strID.Split(',');
        try
        {
            for (int i = 0; i < arrID.Length; i++)
            {
                CmsAccess cAcc = new CmsAccess();
                List<CmsEnty> list = new List<CmsEnty>();
                list = (List<CmsEnty>)cAcc.ListAcc_NewsAll().Where(o => o.ID == OTO.ToInt(arrID[i], 0)).ToList();
                if (list.Count > 0)
                {
                    CmsEnty et = list[0];
                    SqlParameter[] para2 = new SqlParameter[12];
                    para2[0] = new SqlParameter("@AUTHOR", et.AUTHOR);
                    para2[1] = new SqlParameter("@CTIME", et.CTIME);
                    para2[2] = new SqlParameter("@PTIME", et.PTIME);
                    para2[3] = new SqlParameter("@TITLE", et.TITLE);
                    para2[4] = new SqlParameter("@KEYWORD", et.KEYWORD);
                    para2[5] = new SqlParameter("@SUMMARY", et.SUMMARY);
                    para2[6] = new SqlParameter("@CONTENT", et.CONTENT);
                    para2[7] = new SqlParameter("@STEP", 1);
                    para2[8] = new SqlParameter("@ISENABLE", 1);
                    para2[9] = new SqlParameter("@STOCK", et.STOCKCODE);
                    para2[10] = new SqlParameter("@SOURCE", et.SOURCE);
                    para2[11] = new SqlParameter("@REl", System.Data.SqlDbType.BigInt);
                    para2[11].Direction = System.Data.ParameterDirection.Output;

                    int flag;
                    flag = SQLHelper.ExecuteNonQuery(SQLHelper.StrConn2, System.Data.CommandType.StoredProcedure, "sp_SpiderToNews", para2);
                    var oid = para2[11].Value;

                    if (flag == 1)
                    {
                        SqlParameter[] para3 = new SqlParameter[1];
                        para3[0] = new SqlParameter("@StrID", et.ID.ToString());
                        SQLHelper.ExecuteNonQuery(SQLHelper.StrConn, System.Data.CommandType.StoredProcedure, "sp_NewsPost", para3);
                    }
                }
            }
        }
        catch
        { }
        finally
        {
            string key = string.Format(CacheKey.News_CACHE_KEY, "DB", "All");
            Emoney.Spider.Basic.Cache.MoveCache(key);
        }

        Response.Write("<script>alert('审核成功');this.location.href='NewsList.aspx';</script>");
        Response.End();

    }
}
